package dao.impl;

import dao.BaseDao;
import dao.DormbuildingsDao;
import entity.Dorm_buildings;
import entity.Dormitory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DormbuildingsDaoimpl extends BaseDao implements DormbuildingsDao {
    @Override
    public List<Dorm_buildings> cx() throws SQLException, ClassNotFoundException {
     getConnection();
        PreparedStatement pre = null;
        ResultSet ss = null;
        List<Dorm_buildings> list = new ArrayList<>();

        try {

            String sql = "SELECT building_id, building_name, address, manager_id, total_floors FROM dorm_buildings";
            pre = conn.prepareStatement(sql);
            ss = pre.executeQuery();

            while (ss.next()) {
                Dorm_buildings dor = new Dorm_buildings();
                dor.setBuilding_id(ss.getInt("building_id"));
                dor.setBuilding_name(ss.getString("building_name"));
                dor.setAddress(ss.getString("address"));
                dor.setManager_id(ss.getInt("manager_id"));
                dor.setTotal_floors(ss.getInt("total_floors"));
                list.add(dor);
            }
            return list;
        } finally {
            // 关闭资源（按ResultSet → Statement → Connection顺序）
            if (ss != null) {
                try {
                    ss.close();
                } catch (SQLException e) {
                    e.printStackTrace(); // 实际项目中建议使用日志框架
                }
            }
            if (pre != null) {
                try {
                    pre.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
